﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcertSpace
{
    public partial class AjoutGroupe : Form
    {
        ExtracteurHTML extr = new ExtracteurHTML();
        Browser browser_verif = null;

        GestionConcertsMyspace g;
        public AjoutGroupe(ref GestionConcertsMyspace g)
        {
            this.g = g;
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (browser_verif != null)
            {
                browser_verif.Close();
                browser_verif = null;
            }
            this.Close();
        }

        private void AjoutGroupe_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Pas d'espaces dans les URL !
            textBox2.Text = textBox2.Text.Replace(" ", "");

            if (textBox1.Text != "")
            {
                if (textBox2.Text.StartsWith("http://") == true)
                {
                    if (extr.IsValidMyspaceLink(textBox2.Text) == true)
                    {
                        if (g.verifier_si_groupe_existe_deja(textBox1.Text) == -1)
                        {
                            this.Cursor = Cursors.WaitCursor;
                            button1.Enabled = false;
                            button2.Enabled = false;

                            string ancien_texte = button1.Text;

                            if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                            {
                                button1.Text = "Chargement...";
                            }
                            else
                            {
                                button1.Text = "Loading...";
                            }
                            Application.DoEvents();

                            g.ajouterGroupe(textBox1.Text, textBox2.Text);

                            button1.Text = ancien_texte;
                            this.Cursor = Cursors.Default;

                            if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                            {
                                MessageBox.Show("Vous suivez désormais " + textBox1.Text + " !", "Suivre un groupe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show(textBox1.Text + " added !", "Add a new band", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            if (browser_verif != null)
                            {
                                browser_verif.Close();
                                browser_verif = null;
                            }
                            this.Close();
                        }
                        else
                        {
                            if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                            {
                                MessageBox.Show("Vous suivez déjà " + textBox1.Text + " !", "Groupe déjà suivi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show(textBox1.Text + " already on your list !", "Add a new band", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    else
                    {   
                        if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                        {
                            MessageBox.Show("L'URL Myspace n'est pas correct !", "Suivre un groupe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Uncorrect link specified !", "Add a new band", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                else
                {
                    if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                    {
                        MessageBox.Show("L'URL n'est pas un lien Myspace !", "Suivre un groupe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Specified link is not a Myspace link !", "Add a new band", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                if (Application.CurrentCulture.TwoLetterISOLanguageName == "fr")
                {
                    MessageBox.Show("Veuillez indiquer le nom du groupe !", "Suivre un groupe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Please type in the band name !", "Add a new band", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (browser_verif != null)
            {
                if (browser_verif.IsDisposed)
                {
                    browser_verif.Close();
                    browser_verif = null;
                }
            }

            if (browser_verif == null)
            {
                browser_verif = new Browser();
            }
            browser_verif.Show();
            browser_verif.afficherPage(textBox2.Text);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }
    }
}
